package chapter9.section4;

import java.time.*;
import java.util.Calendar;
import java.util.Date;

public class DatetimeDemo {
    public static void main(String[] args) {
        // 获取当前毫秒数示例
        long currentMillis = System.currentTimeMillis();
        System.out.println("当前毫秒数: " + currentMillis);
        System.out.println("转换为Date对象: " + new Date(currentMillis) );

        // 旧API Date类示例
        Date date = new Date();
        System.out.println("\nDate示例:");
        System.out.println("当前时间: " + date);
        
        // 使用SimpleDateFormat格式化Date对象
        java.text.SimpleDateFormat sdf1 = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat("yyyy年MM月dd日 EEE a hh:mm:ss");
        System.out.println("格式化输出1: " + sdf1.format(date));
        System.out.println("格式化输出2: " + sdf2.format(date));

        // 1. LocalDate示例 - 处理日期
        LocalDate today = LocalDate.now();
        System.out.println("\n当前日期: " + today);
        System.out.println("年: " + today.getYear());
        System.out.println("月: " + today.getMonthValue());
        System.out.println("日: " + today.getDayOfMonth());
        
        // 2. LocalTime示例 - 处理时间
        LocalTime currentTime = LocalTime.now();
        System.out.println("\n当前时间: " + currentTime);
        System.out.println("时: " + currentTime.getHour());
        System.out.println("分: " + currentTime.getMinute());
        System.out.println("秒: " + currentTime.getSecond());
        
        // 3. LocalDateTime示例 - 处理日期和时间
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("\n当前日期和时间: " + currentDateTime);
        
        // 4. Calendar示例 - 日历操作
        Calendar calendar = Calendar.getInstance();
        System.out.println("\nCalendar示例:");
        System.out.println("年: " + calendar.get(Calendar.YEAR));
        System.out.println("月: " + (calendar.get(Calendar.MONTH) + 1)); // 月份从0开始
        System.out.println("日: " + calendar.get(Calendar.DAY_OF_MONTH));

        // 日期时间计算和比较演示
        System.out.println("\n日期时间计算和比较:");
        
        // 1. LocalDate日期计算
        LocalDate tomorrow = today.plusDays(1);
        LocalDate lastWeek = today.minusWeeks(1);
        System.out.println("明天日期: " + tomorrow);
        System.out.println("上周今天: " + lastWeek);
        
        // 2. LocalTime时间比较
        LocalTime noon = LocalTime.of(12, 0);
        System.out.println("当前时间(" + currentTime + ")是否在中午之前: " + currentTime.isBefore(noon));
        
        // 3. LocalDateTime日期时间比较
        LocalDateTime yesterday = currentDateTime.minusDays(1);
        System.out.println("当前时间(" + currentDateTime + ")是否在昨天(" + yesterday + ")之后: " + currentDateTime.isAfter(yesterday));
        
        // 4. Calendar日期计算
        calendar.add(Calendar.DAY_OF_MONTH, 30);
        System.out.println("30天后的日期: " + calendar.getTime());

        // Calendar和Date互相转换演示
        System.out.println("\nCalendar和Date转换:");
        // Calendar转Date
        Date calendarToDate = calendar.getTime();
        System.out.println("Calendar转Date: " + calendarToDate);

        // TimeZone时区演示
        System.out.println("\n时区演示:");
        
        // 1. 获取系统默认时区
        ZoneId defaultZone = ZoneId.systemDefault();
        System.out.println("系统默认时区: " + defaultZone);
        
        // 2. 获取指定时区
        ZoneId shanghaiZone = ZoneId.of("Asia/Shanghai");
        ZoneId newYorkZone = ZoneId.of("America/New_York");
        System.out.println("上海时区: " + shanghaiZone);
        System.out.println("纽约时区: " + newYorkZone);
        
        // 3. 显示不同时区的当前时间
        ZonedDateTime shanghaiTime = ZonedDateTime.now(shanghaiZone);
        ZonedDateTime newYorkTime = ZonedDateTime.now(newYorkZone);
        System.out.println("\n当前时间在不同时区:");
        System.out.println("上海: " + shanghaiTime);
        System.out.println("纽约: " + newYorkTime);
        
        // 4. 时区转换示例
        System.out.println("\n时区转换示例:");
        ZonedDateTime convertedTime = shanghaiTime.withZoneSameInstant(newYorkZone);
        System.out.println("上海时间转换为纽约时间: " + convertedTime);
        
        // 5. 显示时区偏移量
        System.out.println("\n时区偏移量:");
        System.out.println("上海时区偏移: " + shanghaiTime.getOffset());
        System.out.println("纽约时区偏移: " + newYorkTime.getOffset());

    }
}